package xcmg.device.web.sale;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.dao.entity.company.CompanyInfoDO;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.company.CompanyInfoService;
import xcmg.device.service.sale.SalesBillOutService;
import xcmg.device.service.vo.delivery.ManualFinishVO;
import xcmg.device.service.vo.sale.SaleOutAddVO;
import xcmg.device.service.vo.sale.SalesOutBillDetailVO;
import xcmg.device.service.vo.sale.SalesOutBillVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by wrg on 2019/4/25.
 */
@RestController
@RequestMapping("/salesBillOut")
@Api("销售出库单入口")
public class SalesBillOutController extends BasicController {

    @Autowired
    private SalesBillOutService salesBillOutService;
    @Autowired
    private CompanyInfoService companyInfoService;

    @GetMapping(value = "/itemBySaleBillNo")
    @ApiOperation("获取销售单详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult itemBySaleBillNo(@RequestParam("salesNo") String salesNo) throws Exception {
        if (StringUtils.isBlank(salesNo)) {
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionValueResult(salesBillOutService.itemBySaleBillNo(salesNo, getOrgId()));
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "增加出库单")
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody SaleOutAddVO saleOutAddVO) throws Exception {
        try {
            if (saleOutAddVO == null ||
                    null == saleOutAddVO.getHeaderInfo() ||
                    null == saleOutAddVO.getMaterialList() ||
                    saleOutAddVO.getMaterialList().isEmpty()) {
                return actionResult(ErrorCode.IllegalArument);
            }
            ErrorCode ret;
            if (StringUtils.isBlank(saleOutAddVO.getHeaderInfo().getSalesOutBillNo())) {
                ret = salesBillOutService.add(saleOutAddVO, getUserId(), getOrgId(), getUserName());
            } else {
                ret = salesBillOutService.update(saleOutAddVO, getUserId(), getOrgId());
            }
            return actionResult(ret);
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "销售出库单")
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody String[] ids) throws Exception {
        if (ids == null || ids.length == 0) {
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode ret = salesBillOutService.remove(Arrays.asList(ids), getOrgId());
        return actionResult(ret);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "销售单列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<SalesOutBillVO> condition) throws Exception {
        if (condition.getFilters() == null) {
            SalesOutBillVO filters = new SalesOutBillVO();
            filters.setOrgId(getOrgId());
            condition.setFilters(filters);
        } else {
            condition.getFilters().setOrgId(getOrgId());
        }
        PageCommonVO pageCommonVO = salesBillOutService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/availableReturnList", method = RequestMethod.POST)
    @ApiOperation(value = "可退货销售单列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult availableReturnList(@RequestBody SearchCommonVO<SalesOutBillVO> condition)
            throws Exception {
        if (condition.getFilters().getOrgId() == null) {
            condition.getFilters().setOrgId(getOrgId());
        } else {
            condition.getFilters().setOrgId(getOrgId());
        }
        PageCommonVO pageCommonVO = salesBillOutService.availableReturnList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @GetMapping(value = "/item")
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "获取销售出库单详情")
    public ActionResult item(String id, boolean isPrint) throws Exception {
        if (StringUtils.isEmpty(id)) {
            return actionResult(ErrorCode.IllegalArument);
        }
        String orgId = getOrgId();
        CompanyInfoDO comapany = companyInfoService.getByOrgId(orgId);
        return actionResult(ErrorCode.Success, salesBillOutService.item(id, isPrint, comapany));
    }

    @GetMapping(value = "/itemList")
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "获取销售出库单详情列表")
    public ActionResult item(String[] ids) throws Exception {
        List<SaleOutAddVO> voList = new ArrayList<>();
        String orgId = getOrgId();
        CompanyInfoDO comapany = companyInfoService.getByOrgId(orgId);
        for (String id : ids
        ) {
            SaleOutAddVO item = salesBillOutService.item(id, true, comapany);
            voList.add(item);
        }
        return actionResult(ErrorCode.Success, voList);
    }

    @PostMapping(value = "/manualFinish")
    @ApiOperation("手动完成销售出库单")
    @FastMappingInfo(needLogin = true)
    public ActionResult manualFinish(@RequestBody ManualFinishVO manualFinishVO) throws Exception {
        return actionResult(salesBillOutService
                .manualFinish(manualFinishVO.getBillNo(), manualFinishVO.getMaterialsNo(), getOrgId()));
    }

    @PostMapping(value = "/export")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("根据勾选数据导出")
    public void export(String[] ids) throws Exception {
        salesBillOutService.export(Arrays.asList(ids));
    }

    @PostMapping(value = "/exportAll")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("导出所有数据")
    public void exportAll(SalesOutBillVO condition) throws Exception {
        if (null == condition) {
            condition = new SalesOutBillVO();
        }
        condition.setOrgId(getOrgId());
        salesBillOutService.exportAll(condition);
    }

    @GetMapping(value = "/getRefundInfo")
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "获取销售出库单详情")
    public ActionResult getRefundInfo(String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(ErrorCode.Success, salesBillOutService.getRefundInfo(id));
    }

//    @RequestMapping(value = "/sapPost", method = RequestMethod.GET)
//    @FastMappingInfo(needLogin = true)
//    @ApiOperation(value = "过账-(施维英)")
//    public ActionResult sapPost(String id) {
//        try {
//            String orgId = getOrgId();
//            return actionResult(salesBillOutService.postSap(id, orgId, getUserId(), getUserName()));
//        } catch (Exception e) {
//            LogHelper.error(e.getMessage(), ErrorCode.Failure.getCode());
//            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
//        }
//    }

//    @PostMapping(value = "/autoSubmit")
//    @ApiOperation(value = "自动提交出库单-进出口")
//    @FastMappingInfo(needLogin = true)
//    public ActionResult autoSubmit(@RequestBody SaleOutAutoAddVO saleOutAutoAddVO) throws Exception {
//        try {
//            if (null == saleOutAutoAddVO
//                    || null == saleOutAutoAddVO.getMaterialList()
//                    || null == saleOutAutoAddVO.getBillType()) {
//                return actionResult(ErrorCode.IllegalArument);
//            }
//            String orgId = getOrgId();
//            ErrorCode ret = salesBillOutService.autoSubmit(saleOutAutoAddVO, getUserId(), orgId, getUserName());
//
//            return actionResult(ret);
//        } catch (Exception e) {
//            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
//        }
//
//    }

    @GetMapping(value = "/listDetailByStatus")
    @ApiOperation(value = "自动提交出库单-进出口")
    @FastMappingInfo(needLogin = true)
    public ActionResult listDetailByStatus() throws Exception {

        List<SalesOutBillDetailVO> detailList = salesBillOutService.listDetailByStatus(getOrgId());
        return actionResult(ErrorCode.Success, detailList);

    }
}
